package com.yl.chainStore.control.warehouse

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.pay.SysCompanyPayItem
import com.yl.chainStore.entity.pay.SysCompanyPayItemType
import com.yl.chainStore.pojo.vo.pay.SystemCompanyPayItemSelectVo
import com.yl.chainStore.pojo.vo.warehouse.UpdateItemConfigVo
import com.yl.chainStore.services.pay.ISysCompanyPayItemService
import com.yl.chainStore.services.pay.ISysCompanyPayItemTypeService
import com.yl.chainStore.services.warehouse.IWarehouseService
import com.yl.chainStore.util.payItemTypeListToTree
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * 物品管理
 */
@RestController
@RequestMapping("warehouse/good")
open class WarehouseGoodsController(
	private val iWarehouseService: IWarehouseService,
	private val iSysCompanyPayItemTypeService: ISysCompanyPayItemTypeService,
	private val iSysCompanyPayItemService: ISysCompanyPayItemService
) : BaseCtrl() {

	/**
	 * 分类树形图
	 */
	@GetMapping("getTypeTree")
	@Transactional
	open fun getTypeTree(): ResponseBody {
		val goodTypeList = iWarehouseService.getGoodTypeList(getContext().subCompanyId)
		return returnSuccess(payItemTypeListToTree(goodTypeList, 0L))
	}

	/**
	 * 添加类型
	 */
	@PostMapping("addType")
	@PermsCode("warehouse:good:type:add")
	@Transactional
	open fun addType(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		type.deptId = getContext().subCompanyId
		return iWarehouseService.addGoodType(type)
	}

	/**
	 * 修改类型
	 */
	@PostMapping("updateType")
	@PermsCode("warehouse:good:type:update")
	@Transactional
	open fun updateType(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		return iSysCompanyPayItemTypeService.update(type)
	}

	/**
	 * 删除类型
	 */
	@PostMapping("delType/{id}")
	@PermsCode("warehouse:good:type:del")
	@Transactional
	open fun updateType(@PathVariable id: Long): ResponseBody {
		return iSysCompanyPayItemTypeService.delType(id)
	}

	/**
	 * 物品列表
	 */
	@PostMapping("getGoodList")
	@PermsCode("warehouse:good:getGoodList")
	@Transactional
	open fun getGoodList(@RequestBody vo: SystemCompanyPayItemSelectVo): ResponseBody {
		vo.haveZeroCount = true
		return returnSuccess(
			iWarehouseService.getGoodPage(
				Page<SysCompanyPayItem>(vo.current, vo.size),
				vo,
				getContext().subCompanyId
			)
		)
	}

	/**
	 * 创建一个物品
	 */
	@PostMapping("createGood")
	@PermsCode("warehouse:good:createGood")
	@Transactional
	open fun createGood(@RequestBody item:SysCompanyPayItem):ResponseBody{
		val vo = UpdateItemConfigVo(
			nickname = item.nickname,
			warningCount = item.warningCount,
			highPrice= item.highPrice,
			warningStatus =item.warningStatus
		)
		return iWarehouseService.addPayItem(item,vo)
	}

	/**
	 * 设置物品私有参数
	 */
	@PostMapping("setGoodConfig")
	@Transactional
	open fun addGood(@RequestBody vo: UpdateItemConfigVo):ResponseBody{
		return iSysCompanyPayItemService.setPayItemDeptConfig(vo)
	}

	/**
	 * 删除物品
	 */
	@PostMapping("del/{id}")
	@PermsCode("warehouse:good:del")
	@Transactional
	open fun del(@PathVariable id:Long):ResponseBody{
		return iSysCompanyPayItemService.delItem(id)
	}

	/**
	 * 获取详情
	 */
	@GetMapping("getGoodDetail/{id}")
	@Transactional
	open fun getGoodDetail(@PathVariable id: Long):ResponseBody{
		val item = iSysCompanyPayItemService.itemDetail(id, getContext().subCompanyId)?:return returnSuccess()
		val config = iSysCompanyPayItemService.getDeptItemDetail(id, getContext().subCompanyId)
		val type = iSysCompanyPayItemTypeService.typeDetail(item.typeId)
		if(config != null){
			item.nickname = config.nickname
		}
		if(type != null){
			item.typeName = type.name
		}
		return returnSuccess(item)
	}

	/**
	 * 修改信息
	 */
	@PostMapping("updateGood")
	@PermsCode("warehouse:good:updateGood")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateGood(@RequestBody item:SysCompanyPayItem):ResponseBody{

		val vo = UpdateItemConfigVo(
			nickname = item.nickname,
			warningCount = item.warningCount,
			highPrice= item.highPrice,
			warningStatus =item.warningStatus,
			showLabel = item.showLabel
		)
		item.type = TypeEnum.PAY_ITEM_GOODS
		val deptItemDetail = iSysCompanyPayItemService.getDeptItemDetail(item.id, getContext().subCompanyId)
		if(deptItemDetail != null && deptItemDetail.deptId == getContext().subCompanyId){
			val updateItem = iSysCompanyPayItemService.updateItem(item)
			if(updateItem.code != ErrorCode.SUCCESS.code){
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
				return updateItem
			}
		}

		vo.itemId = item.id
		return iSysCompanyPayItemService.setPayItemDeptConfig(vo)

	}
}
